package com.srd.phone.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.shengrongdai.srd.R;
import com.srd.phone.utils.wheel.adapter.ArrayWheelAdapter;
import com.srd.phone.utils.wheel.widget.OnWheelChangedListener;
import com.srd.phone.utils.wheel.widget.WheelView;

/**
 * 2016 - 05- 16 3
 */
public class CustomOneDialog extends Dialog implements View.OnClickListener, OnWheelChangedListener {

    private Context mContext;
    private String[] dataArr;
    private WheelView wv_view;
    private OnAddressCListener mOnAddressCListener;

    private String selected;

    private CustomOneDialog(Context context) {
        super(context, R.style.dialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog_loop_view);

        setUpViews();
        setUpListener();
        setUpData();

    }

    public static CustomOneDialog makeDialog(Context context, String[] array, OnAddressCListener onAddressCListener) {
        CustomOneDialog dialog = new CustomOneDialog(context);
        dialog.dataArr = array;
        dialog.mOnAddressCListener = onAddressCListener;
        return dialog;
    }

    private void setUpViews() {
        wv_view = (WheelView) findViewById(R.id.wv_view);
        findViewById(R.id.btn_myinfo_sure).setOnClickListener(this);
        findViewById(R.id.btn_myinfo_cancel).setOnClickListener(this);
        int width = mContext.getResources().getDisplayMetrics().widthPixels;
        wv_view.setLayoutParams(new LinearLayout.LayoutParams(width, -1));
    }

    private void setUpListener() {
        wv_view.addChangingListener(this);
    }

    private void setUpData() {
        wv_view.setViewAdapter(new ArrayWheelAdapter<String>(mContext, dataArr));
        wv_view.setVisibleItems(7);
        selected = dataArr[wv_view.getCurrentItem()];
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_myinfo_sure:
                if (mOnAddressCListener != null)
                    mOnAddressCListener.onClick(selected);
                dismiss();
                break;

            case R.id.btn_myinfo_cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        dataArr = null;
        wv_view = null;
        mOnAddressCListener = null;
        super.dismiss();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        selected = dataArr[newValue];
    }

    public interface OnAddressCListener {
        public void onClick(String itemName);
    }
}
